
package com.example.gorannet;

import com.google.android.gcm.GCMRegistrar;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.widget.Toast;

import java.lang.reflect.Method;

public class SplashScreen extends Activity
{

    // Splash screen timer
    private static int         SPLASH_TIME_OUT   = 2000;

    private ConnectionDetector cd;

    /*
     * For Checking The Connection
     */
    static Boolean      CONNECTION_EXISTS = false;

    private int                flag              = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        cd = new ConnectionDetector(getApplicationContext());
        if (cd.isConnectingToInternet())
        {
            CONNECTION_EXISTS = true;
            if (GCMRegistrar.isRegisteredOnServer(getApplicationContext()))
            {
                Toast.makeText(getApplicationContext(), "Already registered with GCM!", Toast.LENGTH_SHORT).show();
                /*
                 * Intent i = new Intent(getApplicationContext(),
                 * MainActivity.class); startActivity(i);
                 */
                flag = 1;

            }
            if (!GCMRegistrar.isRegisteredOnServer(getApplicationContext()))
            {
                //Toast.makeText(getApplicationContext(), "Not registered with GCM!", 1).show();
                flag = 0;
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Not Registered")
                .setMessage(
                        " Your device is not registered!.. \n "
                                + "Do you want to register your device?!")
                                .setIcon(android.R.drawable.ic_dialog_info)
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                                {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        // Yes button clicked, do something
                                        Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                                        startActivity(i);
                                    }
                                }).setNegativeButton("No", new DialogInterface.OnClickListener()
                                {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which)
                                    {
                                        Toast.makeText(getApplicationContext(), "You can not access to the application without registration", Toast.LENGTH_SHORT).show();
                                        System.exit(0);
                                    }
                                })

                                .show();
                // Intent i = new Intent(getApplicationContext(), MainActivity.class);
                //startActivity(i);
            }

        }
        if (!cd.isConnectingToInternet())
        {
            CONNECTION_EXISTS = false;
            // Put up the Yes/No message box
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Connection Error")
            .setMessage(
                    " No internet connection!.. \n "
                            + "Would you like to connect to \n working internet connection?!")
                            .setIcon(android.R.drawable.ic_dialog_info)
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    // Yes button clicked, do something
                                    flag = 1;
                                    if (!CONNECTION_EXISTS)
                                    {
                                        try
                                        {

                                            Object service = getSystemService("statusbar");
                                            Class<?> statusBarManager = Class
                                                    .forName("android.app.StatusBarManager");
                                            Method expand = statusBarManager.getMethod("expand");
                                            expand.invoke(service);

                                        }
                                        catch (Exception e)
                                        {
                                            Toast.makeText(getApplicationContext(), e.toString(),
                                                    Toast.LENGTH_LONG).show();
                                        }
                                    }
                                }
                            }).setNegativeButton("No", new DialogInterface.OnClickListener()
                            {
                                @Override
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    flag = 1;
                                    Toast.makeText(getApplicationContext(),"Internet connection required!!", Toast.LENGTH_SHORT).show();
                                    if (flag == 1)
                                    {
                                        Intent i = new Intent(SplashScreen.this, login.class);
                                        startActivity(i);
                                        finish();
                                    }
                                }
                            })

                            .show();
            /*
             * Toast.makeText(getApplicationContext(),
             * "Internet Connection Error!.. \n " +
             * "Please connect to working Internet connection", 1).show();
             */
        }
      //  DBHandler db = new DBHandler(getApplicationContext());
        new Handler().postDelayed(new Runnable()
        {
            /*
             * Showing splash screen with a timer. This will be useful when you
             * want to show case your app logo / company
             */

            @Override
            public void run()
            {
                if (CONNECTION_EXISTS == true && flag ==1)
                {
                    Intent i = new Intent(SplashScreen.this, login.class);
                    startActivity(i);
                    finish();
                }
                else
                {
                    if (flag == 1)
                    {
                        Intent i = new Intent(SplashScreen.this, login.class);
                        startActivity(i);
                        finish();
                    }
                }
            }
        }, SPLASH_TIME_OUT);

    }

}
